class GameScene extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();
	}

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}
	/**停牌 */
	public btn_stand: eui.Image;
	/**拿牌 */
	public btn_hit: eui.Image;
	/**筹码 */
	public coin: eui.Group;
	public coin5: eui.Image;
	public coin10: eui.Image;
	public coin25: eui.Image;
	public coin50: eui.Image;
	public coin100: eui.Image;
	/**当前筹码 */
	public current_bet: eui.Group;
	public coin0: eui.Image;
	public coin1: eui.Image;
	public coin2: eui.Image;
	public coin3: eui.Image;
	public coin4: eui.Image;
	/**本金余额 */
	public money: eui.Label;
	/**用户的牌 */
	public player_cards: eui.Group;
	/**庄家的牌 */
	public banker_cards: eui.Group;
	/**结果展示 */
	public tips: eui.Image;


	protected childrenCreated(): void {
		super.childrenCreated();
		this.init();
	}
	private init() {

		this.runGame();

	}
	/**是否第一次点击拿牌按钮 */
	// private isHit: number = 0;
	/**拿牌事件 */
	private onHit() {
		/**玩家加一张牌 数组 显示*/
		// this.player.cards.push(this.getCardIndex());
		let player_card:eui.Image = new eui.Image();
		player_card.source = `${this.getCardByIndex()}_png`;
		player_card.width = 80;
		player_card.height = 110;
		this.player_cards.addChild(player_card);
		/**判断是否爆牌 */
		if (21) {
			/**结束游戏 */
		}
	}


	// }
	/**停牌事件 */
	private onStand() {
		console.log("click Stand!!");
		/**结束游戏 */
	}
	/**重新开始 */
	private startAgain() {

	}
	/**玩家和庄家的实例 */
	private player: Player = new Player();
	private banker: Banker = new Banker();
	/**
	 * 开始游戏
	 */
	private runGame() {

		/**
		 * 初始化界面、玩家庄家等
		 */
		// this.player.cards = [];
		// this.banker.cards = [];
		this.player.bet = 0;
		//清空庄家牌
		this.player_cards.removeChildren();
		//清空玩家牌
		this.banker_cards.removeChildren();
		//清空已下注筹码
		this.current_bet.removeChildren();
		//添加   增加  +下注筹码事件监听
		for (let i = 0; i < this.coin.numChildren; i++) {
			this.coin.getChildAt(i).addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAddBet, this);
		}



	}
	/** 增加下注筹码*/
	private onAddBet(e: egret.TouchEvent) {
		if (parseInt(this.money.text) > 0 && (parseInt(this.money.text)) - e.target.name >= 0) {
			this.btn_hit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onHit, this);
			this.btn_stand.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onStand, this);
			let cut_bet = e.target;
			/**更新本金 */
			this.money.text = `${Number(this.money.text) - cut_bet.name}`;
			let cut_img: eui.Image = new eui.Image();
			/**更新筹码 */
			cut_img.source = e.target.source;
			cut_img.name = e.target.name;
			this.current_bet.addChild(cut_img);
			//添加   增加  -下注筹码事件监听
			for (let i = 0; i < this.current_bet.numChildren; i++) {
				this.current_bet.getChildAt(i).addEventListener(egret.TouchEvent.TOUCH_TAP, this.onReduceBet, this);
			}
		}



	}
	/** 减少下注筹码*/
	private onReduceBet(e: egret.TouchEvent) {

		let cut_bet = e.target;
		let a = parseInt(cut_bet.name);
		let b = parseInt(this.money.text);
		/**更新本金 */
		this.money.text = `${a + b}`;
		/**更新筹码 */
		this.current_bet.removeChild(e.target);
	}
	/**用来标记52张牌是否已经使用过*/
	private array: Array<any> = new Array(52);
	/**取牌随机数*/
	private getCardIndex() {
		var len = this.array.length;
		var index = Math.floor(Math.random() * len);
		var num = this.array[index];
		this.array.splice(index, 1);
		// return num;
		return index;
	}
	/** 黑桃，红桃，梅花，方块，四种花色顺序分别是：A,1,2,....,10,J,Q,K */
	public cards = ['1-1', '1-2', '1-3', '1-4', '1-5', '1-6', '1-7', '1-8', '1-9', '1-10', '1-11', '1-12', '1-13'
		, '2-1', '2-2', '2-3', '2-4', '2-5', '2-6', '2-7', '2-8', '2-9', '2-10', '2-11', '2-12', '2-13'
		, '3-1', '3-2', '3-3', '3-4', '3-5', '3-6', '3-7', '3-8', '3-9', '3-10', '3-11', '3-12', '3-13'
		, '4-1', '4-2', '4-3', '4-4', '4-5', '4-6', '4-7', '4-8', '4-9', '4-10', '4-11', '4-12', '4-13'
		, 'unknown'];
	private getCardByIndex(){
		var len = this.cards.length;
		var index = Math.floor(Math.random() * len);
		var num = this.cards[index];
		this.cards.splice(index, 1);
		return num;
		// return index;
	}
	private getpoint(mycards) {
		var i,
			len = mycards.length,
			sum = 0,
			flag = true;

		for (i = 0; i < len; i++) {
			sum += this.transformToPoint(mycards[i], flag);
		}
		if (sum <= 21) {
			return sum;
		} else {
			sum = 0;
			for (i = 0; i < len; i++) {
				sum += this.transformToPoint(mycards[i], !flag);
			}
			return sum;
		}
	}
	/**
	 * 将cards数组转化为对应的点数
	 * @param i
	 * @param flag 如果flag==true表示A为11，否则A为1
	 * @returns {*}
	 */
	private transformToPoint(i, flag) {
		if (flag) {
			if (i === 0 || i === 13 || i === 26 || i === 39) {
				return 11;
			}
		} else {
			if (i === 0 || i === 13 || i === 26 || i === 39) {
				return 1;
			}
		}

		if (i >= 1 && i <= 12) {
			if (i <= 9) {
				return i + 1;
			} else {
				return 10;
			}
		} else if (i >= 14 && i <= 25) {
			if (i <= 22) {
				return (i - 12);
			} else {
				return 10;
			}
		} else if (i >= 27 && i <= 38) {
			if (i <= 35) {
				return (i - 25);
			} else {
				return 10;
			}
		} else if (i >= 40 && i <= 51) {
			if (i <= 48) {
				return (i - 38);
			} else {
				return 10;
			}
		}
	}


}